<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>作者平台管理</el-breadcrumb-item>
        <el-breadcrumb-item>PC站运营</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :defaults.sync="defaults"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="saveList"
      >
        <template slot-scope="scope" slot="author_idsForm">
          <div>
            <div class="authorId">
              <el-input
              v-model="form.author_ids"
              placeholder="请输入作者ID,多个作者以英文逗号隔开"
            ></el-input>
            <el-button type="primary" size="small" @click.native="getAuthorName"
              >查询</el-button
            >
            </div>
            <div class="">
            <el-input v-model="resData" disabled></el-input>
          </div>
          </div>
        </template>
        <template slot-scope="scope" slot="">
          
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  selectTemplate,
  pcConfigMessageTemplate,
  getAuthorNameByAuthorIds,
  pcConfigMessagePush,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      radio: "1",
      resData: "",
      userInfo: this.$store.state.user.userInfo,
      defaults: {},
      loading: false,
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "edit_time&DESC",
      },
      option: {
        index: true,
        indexLabel: "编号",
        excelBtn: false,
        menu: false,
        border: true,
        column: [
         
          {
            label: "推送作者",
            prop: "push_type",
            type: "radio",
            span: 24,
            // formslot: true,
            row: true,
            dicData: [
              { label: "单发", value: 0 },
              { label: "签约作者", value: 1 },
               { label: "非签约作者", value: 2 },
              { label: "群发", value: 3 },
            ],
          },
          {
            label: "作者ID",
            prop: "author_ids",
            formslot: true,
            type: "input",
            display: true,
            row: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "作者ID必须填写",
              },
            ],
            search: false,
            hide: true,
            addDisplay: true,
            editDisplay: false,
            addDisabled: false,
            editDisabled: true,
          },
          {
            label: "推送内容模板",
            prop: "",
            type: "select",
            dicUrl: `${catUrl + selectTemplate.selectTemplate}`,
            dicFormatter: (res) => {
              return res.source;
            },
            change:(val)=>{
              console.log('value',val)
              this.form.content = val.value
            },
            span: 24,
            row: true,
           
            props: {
              label: "title",
              value: "content",
            },
          },
          {
            label: "自定义内容",
            prop: "content",
            type: "textarea",
            span:24,
             rules: [
              {
                required: true,
                message: "推送内容必须填写",
              },
            ],
          },
          {
            label: "操作人",
            prop: "edit_account",
            type: "input",
            // slot: true,
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
           {
            label: "推送时间",
            prop: "edit_time",
            type: "daterange",
            // formslot: true,
            search: true,
            row: true,
            rules: [
              {
                required: true,
                message: "推送时间必须填写",
              },
            ],
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  watch: {
    "form.push_type"(val) {
      if (val == 0) {
        this.findObject(this.option.column, "author_ids").display = true;
        this.findObject(this.option.column, "authorName").display = true;
      } else {
        this.findObject(this.option.column, "author_ids").display = false;
        this.findObject(this.option.column, "authorName").display = false;
      }
    },
  },
  methods: {
    //获取作者名字
    async getAuthorName() {
      let params = { authorIdList: this.form.author_ids.split(",") };
      let response = await common(
        getAuthorNameByAuthorIds.getAuthorNameByAuthorIds,
        params,
        {},
        "POST"
      );
      console.log(response, "respones");
      if (response.code == 1) {
        this.resData = response.data.join(",");
      }
    },
    async saveList(row, done, loading) {
      let myParams = {
        author_ids:
          this.form.push_type == 0 ? this.form.author_ids.split(",") : [],
        content: this.form.content,
        edit_account: this.userInfo.username,
        edit_user_id: this.userInfo.id,
        push_type: this.form.push_type,
      };
      let response = await common(
        pcConfigMessagePush.save,
        myParams,
        {},
        "POST"
      );
      if (response.code == 1) {
        done();
        this.getList();
      }
      loading();
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        pcConfigMessagePush.pageAndParam,
        {...this.default,...this.params},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      let data = await common(pcConfigMessagePush.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      params.edit_time = `${params.edit_time ? "!<>!" + params.edit_time : ""}`;
      // params.edit_account =  '%'+params.edit_account+'%' //模糊搜索
      // delete params.push_time;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
  mounted() {},
};
</script>
<style scoped>
.authorId {
  /* display: flex; */
}
.nameTitle {
  width: 120px;
}
</style>